<template>
  <div class="message-item" :class="{ 'current-user': isCurrentUser }">
    <div class="message-avatar">
      <img :src="message.avatar || defaultAvatar" alt="User Avatar">
    </div>
    <div class="message-content">
      <div class="message-header">
        <span class="message-username">{{ message.username }}</span>
        <span class="message-time">{{ formattedTime }}</span>
      </div>
      <div class="message-text">{{ message.content }}</div>
    </div>
  </div>
</template>

<script>
import { computed } from 'vue'

export default {
  props: {
    message: {
      type: Object,
      required: true
    },
    isCurrentUser: {
      type: Boolean,
      default: false
    }
  },
  setup(props) {
    const defaultAvatar = 'https://tse4-mm.cn.bing.net/th/id/OIP-C._EJ4gAHvtRGVBotZqOtd4gAAAA?w=166&h=180&c=7&r=0&o=5&pid=1.7'

    const formattedTime = computed(() => {
      const date = new Date(props.message.timestamp)
      return date.toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' })
    })

    return {
      defaultAvatar,
      formattedTime
    }
  }
}
</script>

<style scoped>
.message-item {
  display: flex;
  margin-bottom: 15px;
  padding: 8px 12px;
  border-radius: 8px;
  background-color: rgba(255, 255, 255, 0.05);
  transition: all 0.3s ease;
}

.message-item:hover {
  background-color: rgba(255, 255, 255, 0.1);
}

.message-item.current-user {
  background-color: rgba(76, 201, 240, 0.1);
}

.message-item.current-user:hover {
  background-color: rgba(76, 201, 240, 0.15);
}

.message-avatar {
  width: 40px;
  height: 40px;
  margin-right: 12px;
  flex-shrink: 0;
}

.message-avatar img {
  width: 100%;
  height: 100%;
  border-radius: 50%;
  object-fit: cover;
  border: 2px solid #4cc9f0;
}

.message-content {
  flex: 1;
  min-width: 0;
}

.message-header {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  margin-bottom: 4px;
}

.message-username {
  font-weight: bold;
  color: #4cc9f0;
  font-size: 0.9rem;
}

.current-user .message-username {
  color: #ffffff;
}

.message-time {
  color: #a5a5a5;
  font-size: 0.75rem;
}

.message-text {
  color: #e0e0e0;
  font-size: 0.9rem;
  word-break: break-word;
  white-space: pre-wrap;
}
</style>
